/*
 * @Author: TC (tengchao@taqu.cn)
 * @Date: 2025-10-21 15:08:06
 * @Last Modified by: TC (tengchao@taqu.cn)
 * @Last Modified time: 2025-10-21 15:47:18
 */

import { _decorator, Component, Label, Node, Sprite, tween, v3 } from "cc";
import { app } from "db://assets/FrameWork/App";
import { BaseComponent } from "db://assets/FrameWork/Module/Component/BaseComponent";
import { nsRenderUtil } from "db://assets/FrameWork/Util/RenderUtil";
import { EMoleBundleConst } from "../../Const/MoleBundleConst";
const { ccclass, property } = _decorator;

/**
 * 商城商品item
 */
@ccclass("MoleShopItem")
export class MoleShopItem extends BaseComponent {
    @property({ type: Sprite, displayName: "商品图标" })
    private spIcon: Sprite = null;
    @property({ type: Label, displayName: "商品名称" })
    private lblName: Label = null;
    @property({ type: Label, displayName: "商品价格" })
    private lblPrice: Label = null;

    private _cfg: IMoleCfgInterface.IShop = null;

    protected init(): void {
        super.init();
        this.spIcon.node.setScale(v3(0.9, 0.9, 0.9));
        tween(this.spIcon.node)
            .to(0.8, { scale: v3(0.9, 0.9, 0.9) })
            .to(0.8, { scale: v3(1.1, 1.1, 1.1) })
            .union()
            .repeatForever()
            .start();
    }

    /**
     * 刷新UI
     * @param cfg
     */
    public refreshUI(cfg: IMoleCfgInterface.IShop): void {
        this._cfg = cfg;
        this._refreshIcon();
        // this.lblName.string = cfg.name;
        // this.lblPrice.string = cfg.price.toString();
        this._refreshName();
        this._refreshPrice();
    }

    /**
     * 刷新图标
     */
    private _refreshIcon(): void {
        nsRenderUtil.updateSprite(EMoleBundleConst.Mole, `Icon/${this._cfg.id}`, this.spIcon);
    }

    private _refreshName(): void {
        // this.lblName.string = this._cfg.name;
    }

    /**
     * 刷新价格
     */
    private _refreshPrice(): void {
        const type = this._cfg.type;
        if (1 == type) {
            this.lblPrice.string = this._cfg.price.toString();
        } else if (2 == type) {
            this.lblPrice.string = "asd" + this._cfg.price.toString();
        }
    }
}
